﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinHandleControl
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint wFlags);

        private const int HWND_TOP = 0; //在前面
        private const int HWND_BOTTOM = 1; //在后面
        private const int HWND_TOPMOST = -1; //在前面, 位于任何顶部窗口的前面   
        private const int HWND_NOTOPMOST = -2; //取消前置, 位于其他顶部窗口的后面


        public Form1()
        {
            InitializeComponent();
        }


        public struct WindowRect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        private IntPtr Check()
        {
            String title = textBox1.Text.Trim();
            if (title == "")
            {
                MessageBox.Show("请输入需要设置的窗体名称！");
                return IntPtr.Zero;
            }
            IntPtr mainHandle = FindWindow(null, title); //打印预览
            if (mainHandle == IntPtr.Zero)
            {
                MessageBox.Show("未获取到【" + title + "】窗体句柄！");
                return IntPtr.Zero;
            }
            return mainHandle;
        }

        /// <summary>
        /// 窗体前置
        /// </summary>
        private void btnWindowHander_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            WindowRect rect = new WindowRect();
            SetWindowPos(findHandle, (IntPtr)HWND_TOPMOST, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0);
        }


        /// <summary>
        /// 取消前置
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            WindowRect rect = new WindowRect();
            SetWindowPos(findHandle, (IntPtr)HWND_NOTOPMOST, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0);

        }


        //参数
        private const int SW_HIDE = 0; 　　//从任务栏隐藏
        private const int SW_NORMAL = 1;    //正常弹出窗体 
        private const int SW_MAXIMIZE = 3;    //最大化弹出窗体 
        private const int SW_SHOWNOACTIVATE = 4; 　　//激活窗体/恢复窗体/还原窗体
        private const int SW_SHOW = 5; 　　//显示窗体，最小化时不会最大化
        private const int SW_MINIMIZE = 6; 　　//最小化
        private const int SW_RESTORE = 9;
        private const int SW_SHOWDEFAULT = 10;


        /// <summary>
        /// 最小化(最小化后操作别的好像不起作用了)
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            bool result = ShowWindowAsync(findHandle, 6);
        }

        /// <summary>
        /// 还原
        /// </summary>
        private void button1_Click_1(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            bool result = ShowWindowAsync(findHandle, 4);
        }

        /// <summary>
        /// 最大化
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            bool result = ShowWindowAsync(findHandle, 3);
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            bool result = ShowWindowAsync(findHandle, 0);
        }


        /// <summary>
        /// 显示
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            IntPtr findHandle = Check();
            if (findHandle == IntPtr.Zero)
                return;
            bool result = ShowWindowAsync(findHandle, 5);
        }

    }
}
